package cn.edu.zut.internship.web.controller.sys;


import cn.edu.zut.internship.bean.sys.Result;
import cn.edu.zut.internship.bean.sys.SysUser;
import cn.edu.zut.internship.entity.AuthSysRole;
import cn.edu.zut.internship.iservice.sys.IRoleService;
import cn.edu.zut.internship.web.utils.UserInfoUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private IRoleService roleService;
    @GetMapping("/search")
    public Result search(String roleName){
        Result responseResult = new Result();
        List<AuthSysRole> roleList = roleService.search(roleName);
        responseResult.setBeans(roleList);
        return responseResult;
    }

    @GetMapping("/search/{rlId}")
    public Result search(@PathVariable("rlId") Long rlId){
        Result responseResult = new Result();
        AuthSysRole role = roleService.findById(rlId);
        if(role == null){
            responseResult.setMessage("not exist");
        }else{
            responseResult.getBean().put("role",role);
        }
        return responseResult;
    }
    @PostMapping("/create")
    public Result add(@RequestBody Map role){
        Result responseResult = new Result();
        SysUser userInfo = UserInfoUtil.getUserInfo();
        String creatAccount = userInfo.getUserAccount();
        role.put("creatAccount",creatAccount);
        responseResult = roleService.add(role);
        return responseResult;
    }

    @RequestMapping("/update")
    // 角色的基本信息， 分配角色的路由
    public Result update(@RequestBody Map role){
        Result responseResult = new Result();
        this.roleService.update(role);
        return responseResult;
    }
    @PutMapping("/update/perms")
    // 给角色分配后台的权限
    public Result updatePermssion(@RequestBody AuthSysRole role){
        Result responseResult = new Result();
        this.roleService.updateRolePermissions(role);
        return responseResult;
    }

    @RequestMapping("/delete")
    public Result delete(@RequestBody Map params){
        Result result = new Result();
        this.roleService.deleteRole(params);
        return result;
    }
    @RequestMapping("/selectRoleList")
    public Result selectRoleList(@RequestBody Map params){
        Result result = new Result();
        result = roleService.selectRoleList(params);
        return result;
    }
}
